﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DusonFramework.Documents.Office.Word.OpenXml
{
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Wordprocessing;

    public class Example
    {
        public void CreateSimpleDoc()
        {
            using (WordprocessingDocument package = WordprocessingDocument.Create("Word文件保存路径", WordprocessingDocumentType.Document))
            {
                // Add a new main document part. 
                MainDocumentPart mainDocumentPart = package.AddMainDocumentPart();

                // Create the Document DOM. 
                mainDocumentPart.Document =
                  new Document(
                    new Body(
                      new Paragraph(
                        new Run(
                          new Text("Hello World!")))));

                // Save changes to the main document part. 
                package.MainDocumentPart.Document.Save();

                #region 方法2
                /*
                // Add a new main document part. 
                MainDocumentPart mainPart = package.AddMainDocumentPart();
                //Create Document tree for simple document. 
                mainPart.Document = new Document();
                //Create Body (this element contains other elements that we want to include
                Body body = new Body();
                //Create paragraph
                Paragraph paragraph = new Paragraph();
                Run run_paragraph = new Run();
                // we want to put that text into the output document
                Text text_paragraph = new Text("Hello World!");
                //Append elements appropriately.
                run_paragraph.Append(text_paragraph);
                paragraph.Append(run_paragraph);
                body.Append(paragraph);
                mainPart.Document.Append(body);

                // Save changes to the main document part. 
                mainPart.Document.Save();
                */
                #endregion
            }
        }

        public void Style()
        {
            // Create a Wordprocessing document. 
            using (WordprocessingDocument myDoc = WordprocessingDocument.Create("Word文件保存路径", WordprocessingDocumentType.Document))
            {
                // Add a new main document part. 
                MainDocumentPart mainPart = myDoc.AddMainDocumentPart();

                //Add new style part 
                StyleDefinitionsPart stylePart = mainPart.AddNewPart<StyleDefinitionsPart>();
                // we have to set the properties
                RunProperties rPr = new RunProperties();
                Color color = new Color() { Val = "FF0000" }; // the color is red
                RunFonts rFont = new RunFonts();
                rFont.Ascii = "Arial"; // the font is Arial
                rPr.Append(color);
                rPr.Append(rFont);
                rPr.Append(new Bold()); // it is Bold
                rPr.Append(new FontSize() { Val = "28" }); //font size (in 1/72  of an inch) 
                //creation of a style
                Style style = new Style();
                style.StyleId = "MyHeading1"; //this is the ID of the style
                style.Append(new Name() { Val = "My Heading 1" }); //this is name
                style.Append(new BasedOn() { Val = "Heading1" }); // our style based on Normal style
                style.Append(new NextParagraphStyle() { Val = "Normal" }); // the next paragraph is Normal type
                style.Append(rPr);//we are adding properties previously defined
                // we have to add style that we have created to the StylePart
                stylePart.Styles = new Styles();
                stylePart.Styles.Append(style);
                stylePart.Styles.Save(); // we save the style part

                //Create Document tree for simple document. 
                mainPart.Document = new Document();
                //Create Body (this element contains other elements that we want to include
                Body body = new Body();
                //Create paragraph
                Paragraph paragraph = new Paragraph();
                Run run_paragraph = new Run();
                // we want to put that text into the output document
                Text text_paragraph = new Text("Hello World!");
                //Append elements appropriately.
                run_paragraph.Append(text_paragraph);
                paragraph.Append(run_paragraph);
                Paragraph heading = new Paragraph();
                Run heading_run = new Run();
                Text heading_text = new Text("This is Heading");
                ParagraphProperties heading_pPr = new ParagraphProperties();
                heading_pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "MyHeading1" }; // we set the style
                heading.Append(heading_pPr);
                heading_run.Append(heading_text);
                heading.Append(heading_run);
                body.Append(heading);
                body.Append(paragraph);
                mainPart.Document.Append(body);
                // Save changes to the main document part. 
                mainPart.Document.Save();
            }
        }

        public void Table()
        {
            // Create a Wordprocessing document. 
            using (WordprocessingDocument myDoc = WordprocessingDocument.Create("Word文件保存路径", WordprocessingDocumentType.Document))
            {
                // Add a new main document part. 
                MainDocumentPart mainPart = myDoc.AddMainDocumentPart();
                //Create DOM tree for simple document. 
                mainPart.Document = new Document();
                Body body = new Body();
                Table table = new Table();
                TableProperties tblPr = new TableProperties();
                TableBorders tblBorders = new TableBorders();
                tblBorders.TopBorder = new TopBorder();
                tblBorders.TopBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);
                tblBorders.BottomBorder = new BottomBorder();
                tblBorders.BottomBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);
                tblBorders.LeftBorder = new LeftBorder();
                tblBorders.LeftBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);
                tblBorders.RightBorder = new RightBorder();
                tblBorders.RightBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);
                tblBorders.InsideHorizontalBorder = new InsideHorizontalBorder();
                tblBorders.InsideHorizontalBorder.Val = BorderValues.Single;
                tblBorders.InsideVerticalBorder = new InsideVerticalBorder();
                tblBorders.InsideVerticalBorder.Val = BorderValues.Single;
                tblPr.Append(tblBorders);
                table.Append(tblPr);
                TableRow tr;
                TableCell tc;
                //first row - title
                tr = new TableRow();
                tc = new TableCell(new Paragraph(new Run(new Text("Multiplication table"))));
                TableCellProperties tcp = new TableCellProperties();
                GridSpan gridSpan = new GridSpan();
                gridSpan.Val = 11;
                tcp.Append(gridSpan);
                tc.Append(tcp);
                tr.Append(tc);
                table.Append(tr);
                //second row 
                tr = new TableRow();
                tc = new TableCell();
                tc.Append(new Paragraph(new Run(new Text("*"))));
                tr.Append(tc);
                for (int i = 1; i <= 10; i++)
                {
                    tr.Append(new TableCell(new Paragraph(new Run(new Text(i.ToString())))));
                }
                table.Append(tr);
                for (int i = 1; i <= 10; i++)
                {
                    tr = new TableRow();
                    tr.Append(new TableCell(new Paragraph(new Run(new Text(i.ToString())))));
                    for (int j = 1; j <= 10; j++)
                    {
                        tr.Append(new TableCell(new Paragraph(new Run(new Text((i * j).ToString())))));
                    }
                    table.Append(tr);
                }
                //appending table to body
                body.Append(table);
                // and body to the document
                mainPart.Document.Append(body);
                // Save changes to the main document part. 
                mainPart.Document.Save();
            }
        }

    }
}
